﻿using System;
using DataLayer.Repositories;
using DbEntities;
using DbEntities.Entities;
using DbEntities.Mappings;
using EsbMonitor;
using EsbMonitor.Ninject;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using log4net.Config;
using NHibernate;
using NHibernate.Cfg;
using Ninject;
using NHibernate.Tool.hbm2ddl;

namespace DbTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            
            BasicConfigurator.Configure();

            var initializer = new Initializer();
            initializer.Run();

            var pubRepo = KernelContainer.Kernel.Get<IPublishersRepository>();

            var publisher = new Publisher
                                {
                                    FirstSeen = DateTime.Now,
                                    InputQueueName = "MichalIQN",
                                    LastSeen = DateTime.Now,
                                };

            pubRepo.Add(publisher);

            Console.WriteLine("publisher id: " + publisher.PublisherId);

            Console.ReadLine();




        }

//
//        private static void Main2(string[] args)
//        {
//            BasicConfigurator.Configure();
//
//            // create our NHibernate session factory
//            ISessionFactory sessionFactory = CreateSessionFactory();
//
//            using (ISession session = sessionFactory.OpenSession())
//            {
//                // populate the database
//                using (ITransaction transaction = session.BeginTransaction())
//                {
//                    // create a couple of Stores each with some Products and Employees
//                    var barginBasin = new Node
//                                          {Name = "Bargin Basin", LastSeen = DateTime.Now, FirstSeen = DateTime.Now};
//
//                    // save both stores, this saves everything else via cascading
//                    session.SaveOrUpdate(barginBasin);
//
//                    transaction.Commit();
//                }
//            }
//
//            Console.ReadLine();
//        }


        private static ISessionFactory CreateSessionFactory()
        {
            //   var a= Fluently.Configure();

//            var b = a.Database(PostgreSQLConfiguration.PostgreSQL82
//                           .ConnectionString(c => c
//                                                      .Host("localhost")
//                                                      .Port(5432)
//                                                      .Database("plumber")
//                                                      .Username("postgres")
//                                                      .Password("start")));
//             var d=  b.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Node>().ExportTo("D:\\aa.s"));
//             var nhibConfig = configuration.BuildConfiguration();
//            var e = d.ExposeConfiguration(BuildSchema);
//
//            SchemaMetadataUpdater.QuoteTableAndColumns(e);
//
//                var f = e.BuildSessionFactory();
//
//            return f;


            PostgreSQLConfiguration config = PostgreSQLConfiguration.Standard
                .ConnectionString(c => c
                                           .Host("localhost")
                                           .Port(5432)
                                           .Database("plumber")
                                           .Username("postgres")
                                           .Password("start"));

            FluentConfiguration configuration = Fluently
                .Configure()
                .Database(config)
                .Mappings(m =>
                          m.FluentMappings
                              .AddFromAssemblyOf<PublisherMap>());
            Configuration nhibConfig = configuration.BuildConfiguration();

            configuration.ExposeConfiguration(BuildSchema);
            //SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig);
            return configuration.BuildSessionFactory();
        }

        private static void BuildSchema(Configuration config)
        {
            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            //new SchemaExport(config).Creat(false, true);
            new SchemaUpdate(config).Execute(false, true);
        }
    }
}